package com.airnewslinkosky.ticket.ui.flight;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.adapter.MySimpleAdapter;
import com.airnewslinkosky.ticket.model.FlightInfo;
import com.airnewslinkosky.ticket.model.ItineraryInfo;
import com.airnewslinkosky.ticket.model.SearchFlight;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.sql.FavoriteInfoDB;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.util.Utils;
import com.mobclick.android.MobclickAgent;

public class FlyOrderInput extends BaseActivity implements
		AdapterView.OnItemClickListener, OnClickListener {
	public static List<Map<String,String>> defalutPassengerList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> defalutContactList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> defalutItineraryList = new ArrayList<Map<String,String>>();
	
	public static List<Map<String,String>> passengerList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> contactList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> itineraryList = new ArrayList<Map<String,String>>();
	public static List<Map<String,String>> mileagerInfoList = new ArrayList<Map<String,String>>();
	public static ItineraryInfo itineraryInfo = new ItineraryInfo();
	public static FlightInfo flight;//去程
	public static FlightInfo returnFlight;//回程
	public static SearchFlight searchFlight;
	
	private TextView no_passenger_alert,no_contactor_alert,no_itinerary_alert;
	
	public ItineraryAdapter itinerary_sa;
	public PassengerAdapter passenger_sa;
	public ContactAdapter contactor_sa;
	
	public ListView lv_passenger,lv_contactor,lv_itinerary;
	//直接出票,暂缓出票
	public Button directOutBut,saveBut;

	private ProgressDialog progressDialog = null;
	//登机人 新增,选择登机人
	private Button paddBut,pselectBut,button_update_passenger;
	//联系人 新增,选择登机人
	private Button caddBut,cselectBut,button_update_contactor;
	//收件人新增，选择收件人
	private Button addItineraryBut,selectItineraryBut,button_update_itinerary;
	
	private Button backBut;
	
	List<HashMap<String, Object>> pdata = new ArrayList<HashMap<String, Object>>();
	
	//行程单,保险
	private Button notneedItineraryBut,airItineraryBut,needItineraryBut;
	private Button needInsuranceBut;
	private LinearLayout insuranceDescriptLayout;
	private RelativeLayout itineraryDetailLayout,itineraryTipLayout;
	private RelativeLayout airitineraryTip;
	private boolean isItineraryCheck = true;
	private boolean isInsuranceCheck = true;
	private boolean isAirItineraryCheck = true;
	private boolean isNotneedItineraryCheck = true;
	
	public static String isItinerary = "0";//是否需要行程单
	public static String isInsurance = "0";//是否需要保险
	private TextView itineraryTipOneText,itineraryTipTwoText,itineraryTipThreeText;
	
	private final static int DELETE_DIALOG = 1;
	private final static int DELETE_CONTACTOR_DIALOG = 2;
	private final static int DELETE_ITINERARY_DIALOG =3;
	private static int position;
	
	private Map<String,String> positionSelect;//选定的
	private int editCount = 0;
	private int editContactorCount = 0;
	private int editItineraryCount = 0;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fly_order_passenger_input);
		// 删除窗口背景
        getWindow().setBackgroundDrawable(null);
		activityList.add(this);
		backBut = (Button) findViewById(R.id.backBut);
		backBut.setOnClickListener(this);
		
		needInsuranceBut =  (Button) findViewById(R.id.need_insurance);
		
		notneedItineraryBut =  (Button) findViewById(R.id.notneed_itinerary);
		needItineraryBut =  (Button) findViewById(R.id.need_itinerary);
		
		itineraryDetailLayout = (RelativeLayout) findViewById(R.id.itinerary_detail);
		itineraryTipLayout = (RelativeLayout) findViewById(R.id.itinerary_tip);
		
		
		itineraryTipOneText = (TextView) findViewById(R.id.itinerary_tipone_text);
		itineraryTipTwoText = (TextView) findViewById(R.id.itinerary_tiptwo_text);
		itineraryTipThreeText = (TextView) findViewById(R.id.itinerary_tipthree_text);
		
		if(itineraryList.size() != 0){
			notneedItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
		} else {
			notneedItineraryBut.setBackgroundResource(R.drawable.button_checked);
		}
		
		//保险
		if(isInsurance.equals("0")){
			needInsuranceBut.setBackgroundResource(R.drawable.button_checked);
		} else {
			needInsuranceBut.setBackgroundResource(R.drawable.button_unchecked);
		}
		
		directOutBut = (Button) findViewById(R.id.directOutBut);
		
		paddBut = (Button) findViewById(R.id.button_add_passenger);
		paddBut.setOnClickListener(this);
		
		pselectBut =  (Button) findViewById(R.id.button_get_passenger);
		pselectBut.setOnClickListener(this);
		
		button_update_passenger = (Button) findViewById(R.id.button_update_passenger);
		button_update_passenger.setOnClickListener(this);
		
		caddBut = (Button) findViewById(R.id.button_add_contactor);
		caddBut.setOnClickListener(this);
		
		cselectBut =  (Button) findViewById(R.id.button_get_contactor);
		cselectBut.setOnClickListener(this);
		
		button_update_contactor =  (Button) findViewById(R.id.button_update_contactor);
		button_update_contactor.setOnClickListener(this);
		
		addItineraryBut =  (Button) findViewById(R.id.button_add_itinerary);
		addItineraryBut.setOnClickListener(this);
		selectItineraryBut =  (Button) findViewById(R.id.button_get_itinerary);
		selectItineraryBut.setOnClickListener(this);
		button_update_itinerary =  (Button) findViewById(R.id.button_update_itinerary);
		button_update_itinerary.setOnClickListener(this);
		
		needInsuranceBut.setOnClickListener(this);
		notneedItineraryBut.setOnClickListener(this);
		needItineraryBut.setOnClickListener(this);
		directOutBut.setOnClickListener(this);
		
		defalutPassengerList = FavoriteInfoDB.selectTopOnePsgInfoList(FlyOrderInput.this);
		Log.i("defalutPassengerList.size()", String.valueOf(defalutPassengerList.size()));
		if(passengerList.size() == 0){
			passengerList = defalutPassengerList;
		}
		
		
		defalutContactList = FavoriteInfoDB.selectTopOneContactorInfoList(FlyOrderInput.this);
		if(contactList.size() == 0){
			contactList = defalutContactList;
		}
		
//		defalutItineraryList = FavoriteInfoDB.selectTopOneItineraryInfoList(FlyOrderInput.this);
//		if(itineraryList.size() == 0){
//			itineraryList = defalutItineraryList;
//		}
		
		
		insuranceDescriptLayout =  (LinearLayout) findViewById(R.id.insurance_descript_layout);
		insuranceDescriptLayout.setOnClickListener(this);
		
		//登机人
		lv_passenger = (ListView) findViewById(R.id.passenger_list);
		lv_passenger.setOnItemClickListener(this);
		
		prePassengerAdapterData();
		
		no_passenger_alert = (TextView)findViewById(R.id.no_passenger_alert);
		if(passengerList.size() != 0) {
			no_passenger_alert.setVisibility(View.GONE);
			Utils.setListViewHeightBasedOnChildren(lv_passenger);//更新list高度
			passenger_sa.notifyDataSetChanged();
		} else {
			no_passenger_alert.setVisibility(View.VISIBLE);
			no_passenger_alert.setText("暂未添加登机人 ！");
		}
		
		
		//联系人
		lv_contactor = (ListView) findViewById(R.id.contactor_list);
		lv_contactor.setOnItemClickListener(this);
		preContactAdapterData();
		
		
		no_contactor_alert = (TextView)findViewById(R.id.no_contact_alert);
		if(contactList.size() != 0) {
			no_contactor_alert.setVisibility(View.GONE);
			Utils.setListViewHeightBasedOnChildren(lv_contactor);
			contactor_sa.notifyDataSetChanged();
		} else {
			no_contactor_alert.setVisibility(View.VISIBLE);
			no_contactor_alert.setText("暂未添加联系人 ！");
		}
		
		//收件人
		lv_itinerary = (ListView) findViewById(R.id.itinerary_list);
		lv_itinerary.setOnItemClickListener(this);
		preItineraryAdapterData();
		
		no_itinerary_alert = (TextView)findViewById(R.id.no_itinerary_alert);
		if(itineraryList.size() != 0) {
			no_itinerary_alert.setVisibility(View.GONE);
			needItineraryBut.setBackgroundResource(R.drawable.button_checked);
			itineraryDetailLayout.setVisibility(View.VISIBLE);
			itineraryTipLayout.setVisibility(View.GONE);
			Utils.setListViewHeightBasedOnChildren(lv_itinerary);
			notneedItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
		}  else {
			no_itinerary_alert.setVisibility(View.VISIBLE);
			no_itinerary_alert.setText("暂未添加收件人 ！");
		}
		
//		RadioGroup radioGroup = (RadioGroup) findViewById(R.id.itineraryRadiogroup);
//		radioGroup.check(R.id.notneed_itinerary);
//		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            public void onCheckedChanged(RadioGroup group, int checkedId) {
//                RadioButton radioButton = (RadioButton) findViewById(checkedId);
//                isItinerary = String.valueOf(radioButton.getText());
//                if("不需要".equals(isItinerary)) {
//                	isItinerary = "0";
//                } else if("机场代打".equals(isItinerary)) {
//                	isItinerary = "1";
//                } else if("邮寄(￥20)".equals(isItinerary)) {
//                	isItinerary = "2";
//                }
//                Log.i("isItinerary", isItinerary);
//            }
//        });
		
	}
	private void prePassengerAdapterData() {
		passenger_sa = new PassengerAdapter(
				this,
				passengerList,
				R.layout.flight_passenger_item,
				new String[] { "passengerName", "passengerIdNumber"},
				new int[] { R.id.passenger_name, R.id.passenger_id_number});
		
		lv_passenger.setAdapter(passenger_sa);
		passenger_sa.notifyDataSetChanged();
	}
	
	private void preContactAdapterData(){
		contactor_sa = new ContactAdapter(
				this,
				contactList,
				R.layout.flight_contactor_item,
				new String[] { "contactName", "phone"},
				new int[] { R.id.contactor_name, R.id.contactor_mobile});
		lv_contactor.setAdapter(contactor_sa);
		contactor_sa.notifyDataSetChanged();
	}
	
	private void preItineraryAdapterData(){
		itinerary_sa = new ItineraryAdapter(
				this,
				itineraryList,
				R.layout.flight_itinerary_item,
				new String[] { "consigneeName", "consigneePhone","consigneeAddress"},
				new int[] { R.id.consigneeName, R.id.consigneePhone, R.id.consigneeAddress});
		lv_itinerary.setAdapter(itinerary_sa);
		itinerary_sa.notifyDataSetChanged();
	}
	public void onItemClick(AdapterView<?> arg0, View v, int position,
			long arg3) {
		switch (arg0.getId()) {
			case R.id.passenger_list:
//				this.position = position;
//				showDialog(DELETE_DIALOG);
				
				Intent intenthistory = new Intent(FlyOrderInput.this,PassengerEdit.class);
              	intenthistory.putExtra("position", position);
              	PassengerEdit.flight = flight;
              	PassengerEdit.returnFlight = returnFlight;
              	PassengerEdit.searchFlight = searchFlight;
              	PassengerEdit.data = passengerList;
              	PassengerEdit.mileagerInfoList.clear();
 				startActivity(intenthistory);
 				
				break;
			case R.id.contactor_list:
//				this.position = position;
//				showDialog(DELETE_CONTACTOR_DIALOG);
				
				Intent intenthistory2 = new Intent(FlyOrderInput.this,ContactorEdit.class);
              	intenthistory2.putExtra("position", position);
              	ContactorEdit.flight = flight;
              	ContactorEdit.returnFlight = returnFlight;
              	ContactorEdit.searchFlight = searchFlight;
              	ContactorEdit.data = contactList;
 				startActivity(intenthistory2);
				break;
			case R.id.itinerary_list:
//				this.position = position;
//				showDialog(DELETE_ITINERARY_DIALOG);
				Intent intenthistory3 = new Intent(FlyOrderInput.this,ItineraryInfoEdit.class);
             	intenthistory3.putExtra("position", position);
             	ItineraryInfoEdit.flight = flight;
             	ItineraryInfoEdit.returnFlight = returnFlight;
             	ItineraryInfoEdit.searchFlight = searchFlight;
             	ItineraryInfoEdit.data = itineraryList;
				startActivity(intenthistory3);
				break;
		}
	}
	
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.backBut:
				Intent intentR = new Intent();
				if(returnFlight!=null){
					intentR.setClass(FlyOrderInput.this, FlyRoundDetail.class);
				} else {
					intentR.setClass(FlyOrderInput.this, FlyDetail.class);
				}
				startActivity(intentR);
				break;
			case R.id.button_add_passenger:
				Intent intent = new Intent();
				intent.setClass(FlyOrderInput.this, PassengerAdd.class);
				PassengerAdd.flight = flight;
				PassengerAdd.returnFlight = returnFlight;
				PassengerAdd.searchFlight = searchFlight;
				PassengerAdd.mileagerInfoList.clear();
				startActivity(intent);
				break;
			case R.id.button_update_passenger:
				editCount++;
    			prePassengerAdapterData();
    			break;
			case R.id.button_get_passenger:
				Intent intentGet = new Intent();
				intentGet.setClass(FlyOrderInput.this, PassengerList.class);
				PassengerList.flight = flight;
				PassengerList.returnFlight = returnFlight;
				PassengerList.searchFlight = searchFlight;
				PassengerList.mileagerInfoList = mileagerInfoList;
				startActivity(intentGet);
				break;
			case R.id.button_add_contactor:
				Intent intentContact = new Intent();
				intentContact.setClass(FlyOrderInput.this, ContactorAdd.class);
				ContactorAdd.flight = flight;
				ContactorAdd.returnFlight = returnFlight;
				ContactorAdd.searchFlight = searchFlight;
				ContactorAdd.passengerList = passengerList;
				ContactorAdd.mileagerInfoList = mileagerInfoList;
				ContactorAdd.itineraryList = itineraryList;
				startActivity(intentContact);
				break;
			case R.id.button_update_contactor:
				editContactorCount++;
				preContactAdapterData();
				break;
			case R.id.button_get_contactor:
				Intent intentConGet = new Intent();
				intentConGet.setClass(FlyOrderInput.this, ContactorList.class);
				ContactorList.flight = flight;
				ContactorList.returnFlight = returnFlight;
				ContactorList.searchFlight = searchFlight;
				startActivity(intentConGet);
				break;
			case R.id.button_add_itinerary:
				Intent intentAddItinerary = new Intent();
				intentAddItinerary.setClass(FlyOrderInput.this, ItineraryInfoAdd.class);
				ItineraryInfoAdd.flight = flight;
				ItineraryInfoAdd.returnFlight = returnFlight;
				ItineraryInfoAdd.searchFlight = searchFlight;
				ItineraryInfoAdd.passengerList = passengerList;
				ItineraryInfoAdd.contactList = contactList;
				ItineraryInfoAdd.mileagerInfoList = mileagerInfoList;
				
				startActivity(intentAddItinerary);
				break;
			case R.id.button_update_itinerary:
				editItineraryCount++;
				preItineraryAdapterData();
				break;
			case R.id.button_get_itinerary:
				Intent intentItineraryGet = new Intent();
				intentItineraryGet.setClass(FlyOrderInput.this, ItineraryInfoList.class);
				ItineraryInfoList.flight = flight;
				ItineraryInfoList.returnFlight = returnFlight;
				ItineraryInfoList.searchFlight = searchFlight;
				ItineraryInfoList.passengerList = passengerList;
				ItineraryInfoList.contactList = contactList;
				ItineraryInfoList.mileagerInfoList = mileagerInfoList;
				startActivity(intentItineraryGet);
				break;
			case R.id.need_insurance:
				if(isInsuranceCheck){
					needInsuranceBut.setBackgroundResource(R.drawable.button_checked);
					isInsuranceCheck = false;
					isInsurance = "0";
				}else{
					needInsuranceBut.setBackgroundResource(R.drawable.button_unchecked);
					isInsuranceCheck = true;
					isInsurance = "1";
				}
				break;
			case R.id.notneed_itinerary:
				if(isNotneedItineraryCheck){
					notneedItineraryBut.setBackgroundResource(R.drawable.button_checked);
					needItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
					isNotneedItineraryCheck = false;
					isItinerary = "0";
				}else{
					notneedItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
					needItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
					isNotneedItineraryCheck = true;
					isItinerary = "0";
				}
				itineraryDetailLayout.setVisibility(View.GONE);
				itineraryTipLayout.setVisibility(View.VISIBLE);
				break;
			case R.id.need_itinerary:
				if(isItineraryCheck){
					notneedItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
					needItineraryBut.setBackgroundResource(R.drawable.button_checked);
					isItineraryCheck = false;
					isItinerary = "2";
					itineraryDetailLayout.setVisibility(View.VISIBLE);
					itineraryTipLayout.setVisibility(View.GONE);
				}else{
					notneedItineraryBut.setBackgroundResource(R.drawable.button_checked);
					needItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
					isItineraryCheck = true;
					isItinerary = "0";
					itineraryDetailLayout.setVisibility(View.GONE);
					itineraryTipLayout.setVisibility(View.VISIBLE);
				}
				break;
//			case R.id.air_itinerary:
//				if(isAirItineraryCheck){
//					notneedItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
//					needItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
//					airItineraryBut.setBackgroundResource(R.drawable.button_checked);
//					isAirItineraryCheck = false;
//					isItinerary = "1";
//					itineraryDetailLayout.setVisibility(View.GONE);
//					itineraryTipLayout.setVisibility(View.GONE);
//					airitineraryTip.setVisibility(View.VISIBLE);
//				}else{
//					notneedItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
//					needItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
//					airItineraryBut.setBackgroundResource(R.drawable.button_unchecked);
//					isAirItineraryCheck = true;
//					isItinerary = "0";
//					itineraryTipLayout.setVisibility(View.VISIBLE);
//					itineraryDetailLayout.setVisibility(View.GONE);
//					airitineraryTip.setVisibility(View.GONE);
//				}
//				break;
			case R.id.insurance_descript_layout:
				Intent intentAnim = new Intent(this, AnimInsuranceDetail.class);   
				startActivityForResult(intentAnim, 11);   
				//添加界面切换效果，注意只有Android的2.0(SdkVersion版本号为5)以后的版本才支持   
				int version = Integer.valueOf(android.os.Build.VERSION.SDK);      
				if(version  >= 5) {      
				     overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);  //此为自定义的动画效果，下面两个为系统的动画效果   
				}    
				break;
			case R.id.directOutBut:
				if (passengerList.size() == 0) {
					BaseHelper.showDialog(FlyOrderInput.this, "提示", "至少添加一名乘客",
    						R.drawable.infoicon);
					break;
				} 
				if (contactList.size() == 0) {
					BaseHelper.showDialog(FlyOrderInput.this, "提示", "必须添加一名联系人",
    						R.drawable.infoicon);
					break;
				} 
				if(isItinerary.equals("2")) {
					if (itineraryList.size() == 0) {
						BaseHelper.showDialog(FlyOrderInput.this, "提示", "必须添加一名收件人",
	    						R.drawable.infoicon);
						break;
					} 
				}
				
				if(itineraryList.size() != 0) {
					isItinerary = "2";
				}
					
				Intent intentDirectOut = new Intent();
				intentDirectOut.setClass(this, FlyOrderDetail.class);
				FlyOrderDetail.flight = flight;
				FlyOrderDetail.returnFlight = returnFlight;
				FlyOrderDetail.passengerList = passengerList;
				FlyOrderDetail.contactList = contactList;
				FlyOrderDetail.isItinerary = isItinerary;
				FlyOrderDetail.itineraryList = itineraryList;
				FlyOrderDetail.searchFlight = searchFlight;
				FlyOrderDetail.mileagerInfoList = mileagerInfoList;
				FlyOrderDetail.isInsurance = isInsurance;
				startActivity(intentDirectOut);
				break;
		}
	}
	
	public Dialog onCreateDialog(int id) {
		switch (id) {
			case DELETE_DIALOG:
				return buildDeleteDialog();
			case DELETE_CONTACTOR_DIALOG:
				return buildContactorDeleteDialog();
			case DELETE_ITINERARY_DIALOG:
				return buildItineraryInfoDeleteDialog();
		}
		return null;
	}
	
	private Dialog buildDeleteDialog() {
		 return new AlertDialog.Builder(this)
         .setTitle("请选择")
         .setItems(R.array.dialog_passenger_list, new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                 if(which==0){
                	Intent intenthistory = new Intent(FlyOrderInput.this,PassengerEdit.class);
                  	intenthistory.putExtra("position", position);
                  	PassengerEdit.flight = flight;
                  	PassengerEdit.returnFlight = returnFlight;
                  	PassengerEdit.searchFlight = searchFlight;
                  	PassengerEdit.data = passengerList;
                  	PassengerEdit.mileagerInfoList.clear();
     				startActivity(intenthistory);
                 }else if(which==1){
                	passengerList.remove(position);
             		if(passengerList.size() != 0) {
            			no_passenger_alert.setVisibility(View.GONE);
            			Utils.setListViewHeightBasedOnChildren(lv_passenger);
                    	passenger_sa.notifyDataSetChanged();
            		} else {
            			no_passenger_alert.setVisibility(View.VISIBLE);
            			lv_passenger.setVisibility(View.GONE);
            			no_passenger_alert.setText("暂未添加登机人 ！");
            		}
                 }
             }
         })
         .create();
	}

	private Dialog buildContactorDeleteDialog() {
		 return new AlertDialog.Builder(this)
        .setTitle("请选择")
        .setItems(R.array.dialog_passenger_list, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if(which==0){
                	//编辑
                	Intent intenthistory = new Intent(FlyOrderInput.this,ContactorEdit.class);
                  	intenthistory.putExtra("position", position);
                  	ContactorEdit.flight = flight;
                  	ContactorEdit.returnFlight = returnFlight;
                  	ContactorEdit.searchFlight = searchFlight;
                  	ContactorEdit.data = contactList;
     				startActivity(intenthistory);
                }else if(which==1){
                	//删除
                	contactList.remove(position);
	        		if(contactList.size() != 0) {
	        			no_contactor_alert.setVisibility(View.GONE);
	        			Utils.setListViewHeightBasedOnChildren(lv_contactor);
		           	 	contactor_sa.notifyDataSetChanged();
	           		} else {
	           			no_contactor_alert.setVisibility(View.VISIBLE);
	           			lv_contactor.setVisibility(View.GONE);
	           			no_contactor_alert.setText("暂未添加联系人 ！");
	           		}
                }
            }
        })
        .create();
	}
	
	private Dialog buildItineraryInfoDeleteDialog() {
		 return new AlertDialog.Builder(this)
       .setTitle("请选择")
       .setItems(R.array.dialog_passenger_list, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
               if(which==0){
               	//编辑
               	Intent intenthistory = new Intent(FlyOrderInput.this,ItineraryInfoEdit.class);
                 	intenthistory.putExtra("position", position);
                 	ItineraryInfoEdit.flight = flight;
                 	ItineraryInfoEdit.returnFlight = returnFlight;
                 	ItineraryInfoEdit.searchFlight = searchFlight;
                 	ItineraryInfoEdit.data = itineraryList;
    				startActivity(intenthistory);
               }else if(which==1){
               	//删除
            	    itineraryList.remove(position);
	        		if(itineraryList.size() != 0) {
	        			Utils.setListViewHeightBasedOnChildren(lv_itinerary);
	        			itinerary_sa.notifyDataSetChanged();
	           		} else {
	           			no_itinerary_alert.setVisibility(View.VISIBLE);
	           			lv_itinerary.setVisibility(View.GONE);
	           			no_itinerary_alert.setText("暂未添加收件人 ！");
	           		}
               }
           }
       })
       .create();
	}
	
	private class PassengerAdapter extends MySimpleAdapter {
  		
		public PassengerAdapter(Context context, List<? extends Map<String, ?>> data,
				int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
		}
  		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final View v = super.getView(position, convertView, parent);
			final CheckBox checkPassenger = (CheckBox) v.findViewById(R.id.checkPassenger);
			checkPassenger.setVisibility(View.GONE);
			TextView numberView = (TextView) v.findViewById(R.id.passenger_id_number);
			final String number  = numberView.getText().toString();
			if(editCount%2==1){
				Button delButton  = (Button) v.findViewById(R.id.delete_order_passenger);
				delButton.setVisibility(View.VISIBLE);
				delButton.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						v.setVisibility(View.GONE);
						for(Map<String,String> map :passengerList){
							if(map.get("passengerIdNumber").equals(number)){
								passengerList.remove(map);
//								FavoriteInfoDB.deletePsgInfo(FlyOrderInput.this, map.get("passengerName"),map.get("passengerIDNo"));
								break;
							}
						}
						no_passenger_alert = (TextView)findViewById(R.id.no_passenger_alert);
						if(passengerList.size() != 0) {
							no_passenger_alert.setVisibility(View.GONE);
						} else {
							no_passenger_alert.setVisibility(View.VISIBLE);
							no_passenger_alert.setText("暂未添加登机人 ！");
						}
						Utils.setListViewHeightBasedOnChildren(lv_passenger);//更新list高度
						prePassengerAdapterData();
					}
				});
			}
			
			return v;
		}
  	}
	
	private class ContactAdapter extends MySimpleAdapter {
		public ContactAdapter(Context context, List<? extends Map<String, ?>> data,
				int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
		}
  		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final View v = super.getView(position, convertView, parent);
			final RadioButton checkContactor = (RadioButton) v.findViewById(R.id.checkContactor);
			checkContactor.setVisibility(View.GONE);
			if(editContactorCount%2==1){
				Button delButton  = (Button) v.findViewById(R.id.delete_order_contactor);
				delButton.setVisibility(View.VISIBLE);
				delButton.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						v.setVisibility(View.GONE);
						contactList.clear();
						if(contactList.size() != 0) {
							no_contactor_alert.setVisibility(View.GONE);
						} else {
							no_contactor_alert.setVisibility(View.VISIBLE);
							no_contactor_alert.setText("暂未添加联系人 ！");
						}
						Utils.setListViewHeightBasedOnChildren(lv_contactor);//更新list高度
						prePassengerAdapterData();
					}
				});
			}
			
			return v;
		}
	}
	
	
	private class ItineraryAdapter extends MySimpleAdapter {
		public ItineraryAdapter(Context context, List<? extends Map<String, ?>> data,
				int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
		}
  		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			final View v = super.getView(position, convertView, parent);
			final RadioButton checkItinerary = (RadioButton) v.findViewById(R.id.checkItinerary);
			checkItinerary.setVisibility(View.GONE);
			if(editItineraryCount%2==1){
				Button delButton  = (Button) v.findViewById(R.id.delete_order_itinerary);
				delButton.setVisibility(View.VISIBLE);
				delButton.setOnClickListener(new OnClickListener() {
					public void onClick(View view) {
						v.setVisibility(View.GONE);
						itineraryList.clear();
						if(itineraryList.size() != 0) {
							no_itinerary_alert.setVisibility(View.GONE);
						} else {
							no_itinerary_alert.setVisibility(View.VISIBLE);
							lv_itinerary.setVisibility(View.GONE);
							no_itinerary_alert.setText("暂未添加收件人 ！");
						}
						Utils.setListViewHeightBasedOnChildren(lv_itinerary);//更新list高度
						preItineraryAdapterData();
					}
				});
			}
			
			return v;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
			Intent intent3 = new Intent();
			intent3.setClass(FlyOrderInput.this, FlyDetail.class);
			FlyDetail.searchFlight = searchFlight;
			FlyDetail.flight = flight;
			FlyDetail.flight.cabinItemDetail = null;
			startActivity(intent3);
			
			return true;
		}
		return super.onKeyDown(keyCode, event);
	} 
	
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
